package com.cloud.easy.im.handler.strategy.websocket;

import com.cloud.easy.im.argeement.Agreement;
import com.cloud.easy.im.argeement.AgreementFactory;
import com.cloud.easy.im.argeement.AgreementType;
import com.cloud.easy.im.event.ServerEventListener;
import com.cloud.easy.im.processor.OnlineProcessor;
import com.cloud.easy.im.utils.LocalSendHelper;
import com.cloud.easy.im.handler.strategy.AgreementStrategy;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 处理客户端发送心跳包
 * note：需要查询一次在线队列，用于刷新在线队列过期策略事件
 *
 * @author wangjian
 * @version 0.1.0
 * @create 2020年09月20日5:16 下午
 */
public class AgreementWebSocketCKeepLiveHandler extends AgreementStrategy {

    private static final Logger logger = LoggerFactory.getLogger(AgreementWebSocketCKeepLiveHandler.class);

    public AgreementWebSocketCKeepLiveHandler() {
        super(AgreementType.C.FROM_CLIENT_TYPE_OF_KEEP$ALIVE);
    }

    @Override
    public void handler(Channel session, Agreement dataFromClient, ServerEventListener serverEventListener) throws Exception {
        logger.debug("[WS_KEEP$LIVE]<< 收到客户端" + dataFromClient.getFrom() + "发送的keepLive心跳包");

        //判断用户是否登陆
        if (OnlineProcessor.isOnline(session)) {
            //发送心跳包
            LocalSendHelper.sendData(AgreementFactory.createKeepLiveResponse(dataFromClient.getFrom(),dataFromClient.getProductCode()), null);
            return;
        }
        //发送未登陆消息
        LocalSendHelper.replyDataForUnlogined(session, dataFromClient, null);
    }
}
